package com.lagou.edu.utils;

import com.lagou.edu.anno.Component;
import com.lagou.edu.anno.Configuration;
import com.lagou.edu.anno.Repository;
import com.lagou.edu.anno.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * BeanFactory工具类
 */
public class BeanFactoryUtil {

    public static final String POINT = ".";
    public static final String POINT_CLASS = ".class";
    public static final String SLASH = "/";

    /**
     * 判断aClass是否是配置类
     * @param aClass 配置类class
     * @return       true 是
     */
    public static Class<?> checkConfig(Class<?> aClass) {
        if(aClass.getAnnotation(Configuration.class) == null) {
            throw new RuntimeException(aClass.getName() + " not found @Configuration annotation");
        }
        return aClass;
    }

    /**
     * 递归 获取包扫描路径下的所有类路径
     * @param dir           文件
     * @param scanPackage   包扫描路径
     * @param classNames    类路径容器
     * @throws Exception    异常
     */
    public static  void listDirectory(File dir, String scanPackage, List<String> classNames) throws Exception {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                listDirectory(file, scanPackage + POINT +file.getName(), classNames);
            } else {
                // 获取类路径
                String classPath = new StringBuilder(scanPackage).append(POINT)
                                                                 .append(file.getName().replace(POINT_CLASS,""))
                                                                 .toString();
                classNames.add(classPath);
            }
        }
    }

    /**
     * 获取classId
     * @param name       类名
     * @return           classId
     */
    public static String getClassId(String name) {
        // 根据类名获取classId
        if (Character.isLowerCase(name.charAt(0))) {
            return name;
        } else {
            return changeLower(name);
        }
    }

    /**
     * 首字母转小写
     * @param name 类名
     * @return     类名(首字母小写)
     */
    public static String changeLower(String name) {
        return Character.toLowerCase(name.charAt(0)) + name.substring(1);
    }

    public static boolean checkAnnotation(Class<?> aClass) {
        boolean flag = false;
        if (aClass.getAnnotation(Service.class) != null) {
            flag = true;
        }
        if (aClass.getAnnotation(Repository.class) != null) {
            flag = true;
        }
        if (aClass.getAnnotation(Component.class) != null) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获取classId
     * @param aClass  类
     * @return        classId
     */
    public static String getClassId(Class<?> aClass) {
        String classId = "";
        Service service = aClass.getAnnotation(Service.class);
        if (service != null && service.annotationType().getAnnotation(Component.class) != null) {
            classId = service.value();
        }
        Repository repository = aClass.getAnnotation(Repository.class);
        if (repository != null && repository.annotationType().getAnnotation(Component.class) != null) {
            classId = repository.value();
        }
        Component component = aClass.getAnnotation(Component.class);
        if (component != null) {
            classId = component.value();
        }
        if (classId.length() == 0) {
            classId = BeanFactoryUtil.getClassId(aClass.getSimpleName());
        }
        return classId;
    }

    /**
     * 检查是否是代理对象
     * @param obj 对象
     * @return    结果
     */
    public static Boolean checkProxy(Object obj) {
        Boolean flag = false;
        if (Proxy.isProxyClass(obj.getClass()) || net.sf.cglib.proxy.Proxy.isProxyClass(obj.getClass())){
            flag = true;
        }
        return flag;
    }

}
